package com.hjh.tool.extension

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.SharedPreferencesMigration
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStoreFile

/**
 * 获取DataStore实例
 *
 * @author hjh
 * @since  2021/5/12
 */

fun Context.createDataStore(): DataStore<Preferences> {
    val name = "${this.applicationContext.packageName}_ds_preferences"
    return PreferenceDataStoreFactory.create(
        migrations = listOf(SharedPreferencesMigration(this, name)),
        produceFile = { preferencesDataStoreFile(name) }
    )
}



